package com.lucy.viewer.validator;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import com.lucy.viewer.constant.ViewerConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
@Slf4j
public class CanEditFileValidator implements ConstraintValidator<CanEditFile, String> {
    @Override
    public void initialize(CanEditFile constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String o, ConstraintValidatorContext constraintValidatorContext) {
        String extName = FileUtil.extName(o);
        // 如果为空
        if (Validator.isEmpty(extName)) return false;
        // 如果不在 列表中
        if (ViewerConst.DOC_FILE.contains(extName) && !extName.equals("pdf")) return true;
        // pdf 不允许
        if (extName.equals("md")) return true;
        // 合规
        return false;
    }
}
